package com.cy.cykh.app.view.activity

import android.app.Activity
import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.MenuItem
import android.widget.Toast
import com.cy.cykh.app.adapter.HomeAdapter
import com.cy.cykh.app.cdyapp.R
import com.cy.cykh.app.view.base.BaseActivity
import com.cy.cykh.app.view.fragment.HomePagerFragment
import com.cy.cykh.app.view.fragment.MyInfoFragment
import com.thundersoft.thundersso.plugin.callback.SSOCallbackUtils
import com.thundersoft.thundersso.plugin.utils.SSOUtils
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.view_toolbar.*
import java.util.ArrayList

/**
 * 主界面
 * @author Administrator
 * @FileName
 * @date 2019/1/2 15:55
 */
class HomeActivity : BaseActivity() {

    private lateinit var homeAdapter: HomeAdapter
    private lateinit var  menuItem :MenuItem
    private var waitTime: Long = 2000
    private var touchTime: Long = 0

    companion object {

        @JvmStatic
        fun launch(activity: Activity) {
            val intent = Intent(activity, HomeActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun getContentViewID(): Int {

        return R.layout.activity_home
    }

    override fun initView() {
         //fragment集合
         val fragmentList = getFragmentList()
         val titles = resources.getStringArray(R.array.title)
         val fragmentManager = supportFragmentManager
         homeAdapter = HomeAdapter(fragmentList, titles,fragmentManager)
         viewpager.adapter=homeAdapter

         tv_title.text="首页"

         bottom_navigation.setOnNavigationItemSelectedListener{item ->
             when (item.itemId) {
                 R.id.homepage -> {
                     viewpager.setCurrentItem(0, false)
                     tv_title.text="首页"
                 }
                 R.id.mine -> {
                     viewpager.setCurrentItem(1, false)
                     tv_title.text="我的"
                 }
             }
             true
         }

         viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
             override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
             }
             override fun onPageSelected(position: Int) {
                 tv_title.text=if(position==0) "首页" else "我的"
                 setCheckable(position)

             }
             override fun onPageScrollStateChanged(state: Int) {
             }
         })

    }

    private fun getFragmentList(): List<Fragment> {
        val fragments= ArrayList<Fragment>()
            fragments.add(HomePagerFragment.newInstance())
            fragments.add(MyInfoFragment.newInstance())
        return fragments
    }

    private fun setCheckable(position:Int){
        menuItem = bottom_navigation.menu.getItem(position)
        menuItem.isChecked=true
    }




    private fun showInformation() {
        Log.e("登录人=", SSOCallbackUtils.getInstance().loginUsername + "")
    }
        /**点击两次返回退出*/

    override fun onBackPressed() {
        val currentTime = System.currentTimeMillis()
        if (currentTime - touchTime >= waitTime) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()
            touchTime = currentTime
        } else {
            super.onBackPressed()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        SSOUtils.getInstance().reportLogout(this)
    }
}